(function () {

subGrid = function(param){
	this.isHeader = false;
	isShown = false;	
	var body = document.body;				
	var element = document.createElement("table");	
	element.setAttribute("id", param[0]);	
	body.appendChild(element);	
	table = document.getElementById(param[0]);	
	table.border = "1px";
	table.style.visibility = "hidden";
	}
	
	subGrid.prototype = {				
	
		addHeader:  function(par) {			
	
		
			if (!this.isHeader) {	
			var rowElement = document.createElement("tr");		
			table.appendChild(rowElement);		
			var headerEl = document.body.getElementsByTagName("tr")[numberOfRows];				
				for (var i = 0;i <= 2;i++) {
					var dataElement = document.createElement("th");
					dataElement.innerHTML += par[i];
					headerEl.appendChild(dataElement);					
				};
			
				this.isHeader = true;
				numberOfRows ++;
			}		
		},
	
		addRow:  function(par) {				
			var rowElement = document.createElement("tr");	
			table.appendChild(rowElement);		
			var row = document.body.getElementsByTagName("tr")[numberOfRows];	

			for (var i = 0;i<=2;i++) {
				var dataElement = document.createElement("td");
				dataElement.innerHTML += par[i];
				row.appendChild(dataElement);
			};
			
			numberOfRows++;
		},
				
		showGrid:  function() {	
			table.style.visibility	 = "visible";
			isShown = true;
		},
		
		hideGrid: function() {	
			table.style.visibility = "hidden";	
			isShown = false;
		}
	}
	//adding subGrid for Telerik row
	var academyGrid = new subGrid(["tele"]);
	//Adding elements and second header attempt
	academyGrid.addHeader(["Title","Start Date","Total Students"]);
	academyGrid.addHeader(["Title","Start Date","Total Students"]);
	academyGrid.addRow(["JS2","11 april 2013",400]);
	academyGrid.addRow(["SEO","15 may 2013",1300]);
	academyGrid.addRow(["Slice and Dice","05 april 2013",500]);
	
	//adding subGrid for PMG row	
	var pmgGrid = new subGrid(["pm"]);		
	//Adding elements and second header attempt
	pmgGrid.addHeader(["Title","Start Date","Total Students"]);
	pmgGrid.addHeader(["Title","Start Date","Total Students"]);
	pmgGrid.addRow(["Math","1.01.2012",4100]);
	pmgGrid.addRow(["MATH","2.02.2013",1300]);
	pmgGrid.addRow(["MATH!!!","05 april 2012",5]);
	
	var pmgRos = document.getElementById("PMG");
	var academyRos = document.getElementById("Telerik Academy");
	
	//Event listener for Telerk row	
	academyRos.addEventListener("click", function(evt){
	    evt.preventDefault();	
		if(!isShown){
			table = document.getElementById("tele");	
			academyGrid.showGrid();
			isShown = true;
		}
		else {
			table = document.getElementById("tele");	
			academyGrid.hideGrid();
			isShown = false;
		}
	}, false);
	
	//Event listener for PMG row			
	pmgRos.addEventListener("click", function(evt){
	    evt.preventDefault();
		if(!isShown){
			table = document.getElementById("pm");	
			pmgGrid.showGrid();
			isShown = true;
		}
		else {
			table = document.getElementById("pm");	
			pmgGrid.hideGrid();
			isShown = false;
		}
	}, false);
	
})();
